package com.zhuqi.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author zhuqi
 * User 的实体类
 * 我们自定义登录的方法,因为登录需要返回 UserDetails 对象
 * 这里我们实现了 UserDetails 对象 此时就可以当UserDetails 使用(多态)
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SysUser implements UserDetails, Serializable {
    private Integer userid;

    private String username;

    private String userpwd;

    private String sex;

    private String address;

    private Integer delStatus = 1 ;

    private List<String> authorities;

    /**
     * 将权限放入集合中返回
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (CollectionUtils.isEmpty(authorities)){
            return Collections.emptyList();
        }
        ArrayList<GrantedAuthority> permissionList = new ArrayList<>(this.authorities.size());
        authorities.forEach(s -> permissionList.add(new SimpleGrantedAuthority(s)));
        return permissionList;
    }

    @Override
    public String getUsername (){
        return this.username;
    }

    @Override
    public String getPassword() {
        return this.userpwd;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}